package com.gxpt.app.view;

import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.bean.applydetail_bean;
import com.gxpt.app.bean.normal;
import com.utils.ScreenUtils;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.utils.picasso.PicassoUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_apply_goods_detail)
public class Activity_apply_goods_detail extends BaseActivity {

    private static final int NONE                 = 0;
    private static final int PHOTO_GRAPH          = 1;// 拍照
    private static final int PHOTO_ZOOM           = 2; // 缩放
    private static final int PHOTO_RESOULT        = 3;// 结果
    private static final String IMAGE_UNSPECIFIED = "image/*";

    String tmppicPath    = "";
    String cropedpicPath = "";

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.txt_right)
    TextView txt_right;



    @ViewInject(R.id.toApplydelivey)
    Button toApplydelivey;
    @ViewInject(R.id.put_10)
    TextView put_10;

    @ViewInject(R.id.img_thumb)
    ImageView img_thumb;

    @ViewInject(R.id.txt_goods_name)
    TextView txt_goods_name;

    @ViewInject(R.id.txt_sku_name)
    TextView txt_sku_name;

    @ViewInject(R.id.txt_sell_price)
    TextView txt_sell_price;

    @ViewInject(R.id.txt_buy_num)
    TextView txt_buy_num;

    @ViewInject(R.id.txt_has_pay)
    TextView txt_has_pay;

    @ViewInject(R.id.txt_refund_pay)
    TextView txt_refund_pay;


    @ViewInject(R.id.img_view11)
    ImageView img_view11;

    @ViewInject(R.id.img_view22)
    ImageView img_view22;

    @ViewInject(R.id.img_view33)
    ImageView img_view33;

    @ViewInject(R.id.txt_status)
    TextView txt_status;

    @ViewInject(R.id.txt_address)
    TextView txt_address;



    int manner = 5 ;//分数

    //列表图片
    int pic_view_width = 0;
    int pic_view_hight = 0;


    String service_id;



    //临时变量
    Uri photoURI;//相加拍摄保存路径
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        x.view().inject(this);

        Bundle b = getIntent().getBundleExtra("DATA");
        if(b!=null){
            service_id = b.getString("service_id");
        }
        Logger.i("service_id = " +service_id);
        int ScreenW  = ScreenUtils.getScreenWidth(context);//
        pic_view_width  = (ScreenW- AppUtils.dip2px(context,5*4))/3;
        pic_view_hight  = pic_view_width;

        initView();
        initData();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){

    }


    @Override
    public void initView() {
        txt_title.setText(getString(R.string.txt_pingjia));
        txt_right.setVisibility(View.VISIBLE);
//        android.view.ViewGroup.LayoutParams lp = toPicSel1.getLayoutParams();
//        lp.width  =  pic_view_width;
//        lp.height =  pic_view_hight;
//        toPicSel1.setLayoutParams(lp);
//        toPicSel2.setLayoutParams(lp);
//        toPicSel3.setLayoutParams(lp);


    }



    @Override
    public void initData() {
        toNet_applydetail();
    }

    private void setData(applydetail_bean.applydetail d){
        txt_status.setText(AppUtils.getApplySTUTS(Integer.parseInt(d.status)));
        put_10.setText(d.reason);
        txt_address.setText(d.address_info);
        String[] imgs = d.imgs;
        for(int a=0;a<imgs.length;a++){
            if(a==0){
                PicassoUtils.Picasso_displayImage(context,Constant.IP+imgs[0],pic_view_width,pic_view_hight,img_view11);
                img_view11.setVisibility(View.VISIBLE);
            }
            if(a==1){
                PicassoUtils.Picasso_displayImage(context,Constant.IP+imgs[1],pic_view_width,pic_view_hight,img_view22);
                img_view22.setVisibility(View.VISIBLE);
            }
            if(a==2){
                PicassoUtils.Picasso_displayImage(context,Constant.IP+imgs[2],pic_view_width,pic_view_hight,img_view33);
                img_view33.setVisibility(View.VISIBLE);
            }
        }

        List<applydetail_bean.applydetail.goods_info_de> goods_info = d.goods_info;
        for(int a=0;a<goods_info.size();a++){
            if(a==0){
                PicassoUtils.Picasso_displayImage(context,Constant.IP+goods_info.get(0).thumb,pic_view_width,pic_view_hight,img_thumb);
                txt_goods_name.setText(goods_info.get(0).goods_name);
                txt_sku_name.setText(goods_info.get(0).sku_name);
                txt_sell_price.append(goods_info.get(0).sell_price);
                txt_buy_num.setText("x "+goods_info.get(0).goods_num);
            }
        }
        applydetail_bean.applydetail.order_info_de order_info = d.order_info;
        txt_has_pay.setText(getResources().getString(R.string.f_1)+order_info.has_pay);
        txt_refund_pay.setText(getResources().getString(R.string.f_1)+order_info.refund_pay);


    }

    @Event(value={R.id.toback,R.id.toApplydelivey})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
            case R.id.toApplydelivey:
                Dialog_picsel();
                break;
        }
    }



    EditText editText;
    public void Dialog_picsel(){
        final Dialog dialog=new Dialog(context,R.style.customDialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_delivey, null);
        dialog.setContentView(view);

        editText = (EditText)view.findViewById(R.id.editText) ;

        view.findViewById(R.id.toCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        view.findViewById(R.id.toSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editText.getText().toString().equals("")){
                   T.show(context,getResources().getString(R.string.alert_putisnotnull),1);
                }else{
                    toNet_applydelivey(editText.getText().toString());
                    dialog.dismiss();
                }

            }
        });


        Window dialogWindow = dialog.getWindow();
        dialogWindow.setWindowAnimations(R.style.commonAnimDialogStyle);
        dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialog.show();

    }

//    /////NET
    private void toNet_applydetail(){
        hud.show();
          JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "shopservice");
            jsonBody.put("serviceName",          "applydetail");

            jsonBody.put("member_id",            AppUtils.get_user_id(context));
            jsonBody.put("service_id",           service_id);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_applydetail+" ]");
        HttpUtils.http_post_string(Constant.URL_applydetail,jsonBody.toString(),do_applydetail);
    }

    StringCallback do_applydetail = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                applydetail_bean data = new Gson().fromJson(s, applydetail_bean.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    setData(data.data);
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };


    private void toNet_applydelivey(String deliver_code){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "shopservice");
            jsonBody.put("serviceName",          "applydelivey");

            jsonBody.put("member_id",            AppUtils.get_user_id(context));
            jsonBody.put("service_id",           service_id);
            jsonBody.put("deliver_code",         deliver_code);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_applydelivey+" ]");
        HttpUtils.http_post_string(Constant.URL_applydelivey,jsonBody.toString(),do_applydelivey);
    }

    StringCallback do_applydelivey = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                normal data = new Gson().fromJson(s, normal.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    T.show(context,getResources().getString(R.string.alert_success_do),1);
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };

}
